<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="许可证密钥（唯一）" prop="licenseKey">
        <el-input v-model="dataForm.licenseKey" :readonly="dataForm.id" placeholder="许可证密钥（唯一）"></el-input>
        <el-button v-if="!dataForm.id" type="primary" @click="generateLicenseKey">生成密钥</el-button>
      </el-form-item>
      <el-form-item v-if="!dataForm.id" label="允许激活总数" prop="totalActivations">
        <el-input v-model="dataForm.totalActivations" placeholder="允许激活总数"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.id" label="允许激活总数" prop="totalActivations">
        <el-input v-model="dataForm.totalActivations" placeholder="允许激活总数"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.id" label="已激活数量" prop="usedActivations">
        <el-input v-model="dataForm.usedActivations" placeholder="已激活数量"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.id" label="是否有效（1-有效，0-无效）" prop="valid">
        <el-input v-model="dataForm.valid" placeholder="是否有效（1-有效，0-无效）"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.id" label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" :readonly="dataForm.id" placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.id" label="过期时间" prop="expireTime">
        <el-input v-model="dataForm.expireTime" placeholder="过期时间"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.id" label="乐观锁版本号" prop="version">
        <el-input v-model="dataForm.version" placeholder="乐观锁版本号"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          licenseKey: '',
          totalActivations: '',
          usedActivations: '',
          valid: '',
          createTime: '',
          expireTime: '',
          version: ''
        },
        dataRule: {
          licenseKey: [
            { required: true, message: '许可证密钥（唯一）不能为空', trigger: 'blur' }
          ],
          totalActivations: [
            { required: true, message: '允许激活总数不能为空', trigger: 'blur' }
          ],
          usedActivations: [
            { required: true, message: '已激活数量不能为空', trigger: 'blur' }
          ],
          valid: [
            { required: true, message: '是否有效（1-有效，0-无效）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          // expireTime: [
          //   { required: true, message: '过期时间不能为空', trigger: 'blur' }
          // ],
          version: [
            { required: true, message: '乐观锁版本号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/authorization/license/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.licenseKey = data.license.licenseKey
                this.dataForm.totalActivations = data.license.totalActivations
                this.dataForm.usedActivations = data.license.usedActivations
                this.dataForm.valid = data.license.valid
                this.dataForm.createTime = data.license.createTime
                this.dataForm.expireTime = data.license.expireTime
                this.dataForm.version = data.license.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/authorization/license/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'licenseKey': this.dataForm.licenseKey,
                'totalActivations': this.dataForm.totalActivations,
                'usedActivations': this.dataForm.usedActivations,
                'valid': this.dataForm.valid,
                'createTime': this.dataForm.createTime,
                'expireTime': this.dataForm.expireTime,
                'version': this.dataForm.version
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 生成许可证密钥
      generateLicenseKey () {
        this.$http({
          url: this.$http.adornUrl('/authorization/license/generate'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.licenseKey = data.licenseKey
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>